package com.example.chat

import com.example.chat.dao.MessageDao
import com.example.chat.dao.TopicDao
import com.example.chat.model.*
import com.example.chat.dao.TopicParticipantDao
import com.example.featuresdk.database.DatabaseCallback
import com.example.featuresdk.model.User
import com.example.featuresdk.dao.UserDao
import java.sql.Timestamp
import javax.inject.Inject

class ChatDatabaseCallback
@Inject constructor(private val topicDao: TopicDao,
                    private val topicParticipantDao: TopicParticipantDao,
                    private val messageDao: MessageDao,
                    private val userDao: UserDao
): DatabaseCallback() {

    init {
        this.priority = Int.MAX_VALUE
    }

    override suspend fun initialize() {

        super.initialize()

        val users = userDao.getAll()

        //initialize topics for a subset of users
        val topics = mutableListOf<Topic>().apply {
            //create a personal topic for each contact using the user id as topic id
            addAll(users.subList(1,10).map {
                Topic(it.id!!,it.nickname, TopicCategory.Personal, logo = it.icon)
            })

            //create 2 group topics
            add(Topic(10000,"hot topic", TopicCategory.Group, logo = R.drawable.ic_teradata))
            add(Topic(10001,"great news", TopicCategory.Group, logo = R.drawable.ic_firebird))
        }
        topicDao.insert(topics)

        //initialize topic participants
        val topicParticipants = mutableListOf<TopicParticipant>().apply {
            addAll(users.subList(1,10).map {
                TopicParticipant(it.id!!, it.id!!, it.nickname)
            })

            addAll(users.subList(8,15).map {
                TopicParticipant(it.id!!, 10000, it.nickname)
            })

            addAll(users.subList(4,12).map {
                TopicParticipant(it.id!!, 10001, it.nickname)
            })
        }
        topicParticipantDao.insert(topicParticipants)

        //initialize some messages for some personal topics
        val currentTimeMillis = System.currentTimeMillis() - 20000

        //initialize messages for some personal topics
        val p1Messages = topicParticipants.filter { it -> it.userId < 5L }.map {
            Message(null, it.topicId,it.userId,"hi, personal greeting from ${it.username}",null, MessageStatus.New,
                Timestamp(currentTimeMillis)
            )
        }
        messageDao.insert(p1Messages)
        p1Messages.forEach {
            topicDao.updateNewCount(it.topicId)
            topicDao.update(it.topicId,it.text?:"", it.timestamp)
        }

        //initialize messages for group1
        val group1Messages = topicParticipants.filter { it -> it.topicId == 10000L }.map {
            Message(null, it.topicId,it.userId,"hi, greeting from ${it.username}",null, MessageStatus.New,
                Timestamp(currentTimeMillis)
            )
        }
        messageDao.insert(group1Messages)
        topicDao.updateNewCount(10000L)
        group1Messages.last().apply {
            topicDao.update(this.topicId,this.text?:"", this.timestamp)
        }

        messageDao.insert(
            Message(null, 5,
                User.ME.id!!,"Hi, how are you?",null, MessageStatus.Read,
                Timestamp(currentTimeMillis - 5000)
            )
        )
        messageDao.insert(
            Message(null, 5,5,"Great, thanks!",null, MessageStatus.New,
                Timestamp(currentTimeMillis + 5000)
            )
        )
        topicDao.update(5,"Great, thanks!", Timestamp(currentTimeMillis + 5000))
        topicDao.updateNewCount(5L)
    }
}